<?php

namespace App\Http\Controllers\Frontend;

use App\Feedback;
use App\Http\Controllers\Controller;
use App\Http\Util\Common;
use App\Mail\ContactSubmitted;
use App\Photo;
use App\Video;
use Illuminate\Support\Facades\Mail;

class HomeController extends Controller
{
    public function index()
    {
        $slides = Photo::where('is_slide',1)->get();
        return view('frontend.'.'.index',compact('slides'));
    }

    public function photo($type)
    {
        $photos = $type->photos()->orderBy('weight','desc')->paginate(10);
        return view('frontend.photos',compact('photos','type'));
    }

    public function videos()
    {
        $videos = Video::paginate(10);
        return view('frontend.videos',compact('videos'));
    }

    public function contact()
    {
        request()->validate([
            'name' => 'required',
            'content' => 'required',
            'email' => 'required|email'
        ]);

        $data = request()->all();
        $data['type'] = 1;

        Feedback::create($data);

        $email = config('mail.send_to');
        $content = $data['content'];
        $content .= '<br> from email:'.$data['email'];
        $from = config('mail.username');
        $data = ['email' => $email, 'content' => $content, 'from' => $from];

        Mail::to($data['email'])->queue(new ContactSubmitted($data));

        return redirect()->back();
    }

    public function showTestimonials()
    {
        $testimonials = Feedback::where('type',0)->where('review',1)->get();
        return view('frontend.testimonials',compact('testimonials'));
    }

    public function testimonials()
    {
        request()->validate([
            'name' => 'required',
            'content' => 'required',
            'email' => 'required|email'
        ]);

        $data = request()->all();

        if(request()->hasFile('file')) {
            $file = request()->file('file');
            $bool = Common::upload($file);
            $data['image'] = $bool;
            unset($data['file']);
        }
        $order = array("\r\n", "\n", "\r");
        $replace = '<br>';

        $data['content'] = str_replace($order, $replace, $data['content']);
        Feedback::create($data);

        return redirect()->back();
    }
}
